// @algorithm @lc id=100294 lang=cpp
// @title lian-biao-zhong-dao-shu-di-kge-jie-dian-lcof

#include "algm/algm.h"
#include <iostream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *getKthFromEnd(ListNode *head, int k) {
        stack<ListNode *> s;
        ListNode *ans;
        if (head == nullptr) {
            return nullptr;
        }
        while (head != nullptr) {
            s.push(head);
            head = head->next;
        }
        if (k > (int)s.size()) {
            return nullptr;
        }
        while (k--) {
            ans = s.top();
            s.pop();
        }
        return ans;
    }
};